package {
	import demo.isolib.SkinDescriptor;
	import demo.isolib.SpriteSheet;
	import demo.isolib.Tile;
	import demo.isolib.World;
	import demo.isolib.assets.Assets;
	import demo.isolib.assets.TestingUI;
	import demo.isolib.movement.AIMove;
	import demo.isolib.movement.SmoothMove;
	import demo.isolib.movement.Wander;
	
	import flash.display.Sprite;
	import flash.events.Event;

	public class Extended_v1_final extends Sprite {
		public static const WORLD_WDITH:Number = 800;
		public static const WORLD_HEIGHT:Number = 600;

		public static const GRID_CELLSIZE:Number = 10;
		public static const WORLD_COLS:Number = 80;
		public static const WORLD_ROWS:Number = 80;

		private var _world:World;

		public function Extended_v1_final():void {
			trace("new Extended_v1_final");
			super.addEventListener(Event.ADDED_TO_STAGE, init);
		}

		private function init(e:Event = null):void {
			super.removeEventListener(Event.ADDED_TO_STAGE, init);

			this.createWorld();
			//this.createTestingUI();
			this.createObjects();
			this.createCharacters();
		}

		private function createWorld():void {
			//1. buat instance world
			this._world = new World(WORLD_WDITH, WORLD_HEIGHT, WORLD_COLS, WORLD_ROWS, GRID_CELLSIZE);
			//2. set background. offsets : -1175, -900
			this._world.setBackground(new Assets.WORLD_BACKGROUND(), -1175, -900);
			//this._world.setBackground(new Assets.WORLD_BACKGROUND(), 0, 0);			
			//3. set debug mode
			this._world.debugMode = true;
			//4. panning 0, 250
			this._world.panTo(0, 250);
			//5. tambahkan ke display list
			addChild(this._world);
		}

		private function createTestingUI():void {
			var t:Tile = new Tile(2, 2, 100);
			t.name = "button 0";
			t.selectable = false;
			t.addSkin(new TestingUI.BUTTON());
			this._world.addTile(t, 20, 30);
		}

		private function createObjects():void {
			//////////////////////
			// TREE 2x2x100 di 11,19
			//////////////////////
			//1. buat tile 
			var t1:Tile = new Tile(2, 2, 100);
			t1.name = "tree 0";
			//2. tambahkan skin. skala : 0.5, offsets: -40,-110
			t1.addSkin(new Assets.TREE_0(), new SkinDescriptor(-40, -110, 0.5, 0.5));
			//3. tambahkan tree ke world
			_world.addTile(t1, 11, 19);

			return;
			//////////////////////
			// AWNING 9x6x200 di 10,10
			//////////////////////
			//1. buat tile 
			var awning:Tile = new Tile(9, 6, 200);
			awning.name = "awning";
			//2. non-selectable
			awning.selectable = false;
			//3. tambahkan skin, offsets: -130, -116
			awning.addSkin(new Assets.AWNING(), new SkinDescriptor(-130, -116, 1, 1));
			//4. tambahkan ke world
			_world.addTile(awning, 10, 10);

			//////////////////////
			// BARRIER : 5x1x50 di 10,18
			//////////////////////
			//1. buat tile
			var barrier:Tile = new Tile(5, 1, 50);
			barrier.name = "barrier";
			//2. tambahkan skin
			barrier.addSkin(new Assets.BARRIER());
			//3. tambahkan tile ke world
			_world.addTile(barrier, 10, 18);

			//////////////////////
			// CRATE : 2x2x40 di 21,10
			//////////////////////
			//1. buat tile
			var crate:Tile = new Tile(2, 2, 40);
			crate.name = "crate";
			//2. tambahkan skin
			crate.addSkin(new Assets.CRATE());
			//3. tambahkan tile ke world
			_world.addTile(crate, 21, 10);

			//////////////////////
			// GRASS : 1x1x10 di 0,18
			//////////////////////
			//1. buat tile
			var grass1:Tile = new Tile(1, 1, 10);
			grass1.name = "Grass 1";
			//2. tambahkan skin
			grass1.addSkin(new Assets.GRASS_1());
			//3. tambahkan tile ke world
			_world.addTile(grass1, 0, 18);
		}

		private function createCharacters():void {
			//////////////////////
			// PLAYER CHARACTER : 1x1x80 di 15,19
			//////////////////////
			//1. buat animated tile, the avatar.
			var avatar:Tile = createAnimatedTile(1, 1, 80, Assets.MALE_0, Assets.CHAR_DESCRIPTOR);
			avatar.name = "Boy0";
			//2. set movebehavior = AIMove
			//b0.moveBehavior = new AIMove();
			avatar.moveBehavior = new SmoothMove(50);
			avatar.mouseOverHandler = null;
			avatar.mouseOutHandler = null;
			//3. tambahkan ke world
			_world.addTile(avatar, 15, 19);
			
			
			
			
			//////////////////////
			// BOT di 19,19
			//////////////////////

			var g0:Tile = createAnimatedTile(1, 1, 80, Assets.FEMALE_1, Assets.CHAR_DESCRIPTOR)
			g0.name = "Girl0";
			g0.selectable = false;
			g0.moveBehavior = new Wander(1000);
			g0.moveBehavior.execute();
			_world.addTile(g0, 19, 19);

			//////////////////////
			// BOT di 20,20
			//////////////////////

			var g1:Tile = createAnimatedTile(1, 1, 80, Assets.FEMALE_0, Assets.CHAR_DESCRIPTOR)
			g1.name = "Girl1";
			g1.selectable = false;
			g1.moveBehavior = new Wander(2000);
			g1.moveBehavior.execute();
			_world.addTile(g1, 20, 20);
		}
		
		private function createAnimatedTile(cspan:Number, rspan:Number, height:Number, spriteSheetClass:Class, sheetDescriptor:Object):Tile {
			//1. buat tile
			var t:Tile = new Tile(cspan, rspan, height);
			//2. buat spritesheet
			var s:SpriteSheet = new SpriteSheet();
			s.build(new spriteSheetClass(), sheetDescriptor.frameWidth, sheetDescriptor.frameHeight);
			//3. tambahkan spritesheet ke tile
			t.addAnimationSheet(s, new SkinDescriptor(sheetDescriptor.x, sheetDescriptor.y, sheetDescriptor.scaleX, sheetDescriptor.scaleY));

			return t;
		}
	}
}